package textconvert;

/**
 *  xxxxxx<%=$%>hhhhhhhh<%=$%>cccccc<%$%>eeee
 * @author Bing
 * @copyright (c)
 *
 */
public class FormatRule {
	public static final byte[] TAG_S = {'<', '%'};
	public static final byte[] TAG_E = {'%','>'};
	
	public static final int MAX_LEN = 1024;
	
	public static final int STATIC_TEXT = -1;
	public static final int VAR0 = 0; //<%$%>
	public static final int VAR1 = 1; //<%=$%>
	
	ConstByteArray rule;	
	private ConstByteArray value = null;
	private FormatRule next = null;
	
	public FormatRule(ConstByteArray s){
		this(null, s);
	}
	
	public FormatRule(FormatRule prevSibling, ConstByteArray s){
		this.rule = s;
		if(prevSibling!=null){
			prevSibling.next = this;
		}
	}
	
	public FormatRule getNext(){
		return this.next;
	}
	public void addNext(FormatRule r){
		this.next = r;
	}
	
	public boolean matches(ConstByteArray s){
		if(this.rule.regionMatches(TAG_S, 0, TAG_S.length))
			return true;
		else
			return this.rule.equals(s);
	}
	
	public int length(){
		return rule.length();
	}
	
	public int getType(){
		if(this.rule.regionMatches(TAG_S, 0, TAG_S.length)){
			return rule.byteAt(TAG_S.length)=='=' ? VAR1 : VAR0;
		}
		return STATIC_TEXT;
	}
	
	/**
	 * note v maybe changed, so value will also be changed.
	 * @param v
	 */
	void setValue(ConstByteArray v){
		this.value = v;
	}
}
